const checkUtil = require('../../../utils/checkUtil.js')

let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: "",
        settleAmount: "",
        btnDisable: false,
        // 客户数据
        customerData: [],
        customerDataShow: false,

        //客户id
        customerId: "",
        //收货人信息
        customerName: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            this.setData({
                id: options.id,
                spinShow: true,
                queryParam: {},
            });
            this.queryData()
        }
    },
    queryData() {
        let _this = this
        app.formGet('/app/customer/bill/detail?id=' + this.data.id, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let customerBill = res.data
                _this.setData({
                    "id": customerBill.id,
                    "customerId": customerBill.customerId,
                    "receiver": customerBill.receiver,
                    "rphone": customerBill.rphone,
                    "rarea": customerBill.rarea,
                    "raddress": customerBill.raddress,
                    "settleAmount": customerBill.settleAmount
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    // 保存
    handleSaveOrder() {
        let _this = this
        if (!checkUtil.isNumber(this.data.settleAmount)) {
            app.toast("请输入数字", "fail")
            return
        }

        _this.setData({
            btnDisable: true
        })
        let data = {}
        data.id = this.data.id
        data.customerId = this.data.customerId
        data.settleAmount = this.data.settleAmount
        app.formPost('/app/customer/bill/add', data).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                app.message(res.message, 'success')
                setTimeout(function () {
                    wx.redirectTo({
                        url: "/pages/customer/bill/customerBill?customerId=" + _this.data.customerId
                    })
                }, 2000)
            } else {
                app.message(res.message, 'error')
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false,
                btnDisable: false
            });
            app.message(e, 'error')
        })
    },
    // 客户确认选择
    confirmCustomer(e) {
        console.log(e)
        let _this = this
        let index = e.currentTarget.id
        let customer = _this.data.customerData[index]
        _this.setData({
            customerDataShow: false,
            "customerId": customer.id,
            "receiver": customer.receiver,
            "rphone": customer.rphone,
            "rarea": customer.rarea,
            "raddress": customer.raddress
        })
    },
    // 搜索客户
    onSearchCustomer() {
        let _this = this
        app.formGet('/app/customer/list?searchVal=' + this.data.searchVal, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let customerList = res.data.list
                _this.setData({
                    customerData: customerList,
                    customerDataShow: true
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    //选择客户
    chooseCustomer: function () {
        wx.navigateTo({
            url: "/pages/customer/choose/customerChoose"
        })
    },
})